<template>
  <div class="login-form-container">
    <div class="title">登录</div>
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item class="input" prop="account">
        <el-input placeholder="账号" v-model="form.account"></el-input>
      </el-form-item>
      <el-form-item class="input password" prop="password">
        <el-input
          type="password"
          placeholder="密码"
          v-model="form.password"
        ></el-input>
      </el-form-item>
      <div
        class="error-message"
        :style="{
          display: !error ? 'none' : 'block',
        }"
      >
        {{ errorMessage }}
      </div>
      <el-button
        type="primary"
        class="button"
        ref="button"
        @click="handleSubmit"
        >登录</el-button
      >
    </el-form>
  </div>
</template>

<script>
import getExplorer from "@/utils/getExplorer";
import axios from "axios";
import baseURL from "@/common/baseURL";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value.trim() === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    var validateAccount = (rule, value, callback) => {
      if (value.trim() === "") {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    return {
      form: {
        account: "",
        password: "",
      },
      rules: {
        account: [{ validator: validateAccount, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
      },
      errorMessage: "haha",
      error: false,
    };
  },
  mounted() {
    window.addEventListener("keypress", (e) => {
      if (e.key === "Enter") {
        this.handleSubmit();
      }
    });
  },
  methods: {
    async login(formData) {
      const resp = await axios.post(baseURL + "/v1/ent/login/login", {
        username: formData.account,
        password: formData.password,
        browser: getExplorer(),
      });
      return resp;
    },
    async handleSubmit() {
      this.$refs["form"].validate(async (valid) => {
        if (!valid) {
          this.error = false;
          this.$message.error("请输入所有表单项");
          return;
        }
        const resp = await this.login(this.form);
        if (resp.data.success) {
          console.log(resp.data);
          this.$message.success("登录成功");
          this.error=false;
        } else {
          this.error = true;
          this.errorMessage = "请输入正确的账号密码";
          this.$message.error(
            "登录失败" + (resp.data.info ? "，" + resp.data.info : "")
          );
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login-form-container {
  $height: 400px;
  position: absolute;
  padding: 50px 30px;
  left: 50%;
  top: Max($height/2+40px, 50%);
  transform: translate(-50%, -50%);
  width: 450px;
  height: $height;
  background: white;
  border-radius: 30px;
  .title {
    font-size: 32px;
    font-weight: bold;
    text-align: center;
    padding-bottom: 5px;
    border-bottom: 1px solid #aaa;
    margin-bottom: 40px;
  }
  .input {
    margin-top: 30px;
  }
  .button {
    width: 100%;
    height: 50px;
    font-size: 22px;
    font-weight: bold;
    margin-top: 15px;
  }
  .error-message {
    color: red;
  }
}
</style>
